package capaDatos;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import capaLogica.PerfilConcurso;
import capaLogica.PerfilFestival;
import capaLogica.PerfilGrupo;
import capaLogica.PerfilSala;
import capaLogica.Usuario;

public class PerfilDAO {

	/**
	 * Atributo que crea la conexion a la bd
	 */
	private Connection conn;
	
	/**
	 * Constructor de la clase
	 */
	public PerfilDAO(){
		conn = (Connection) ConnectionManager.getInstance().checkOut();
	}
	
	@Override
	 protected void finalize() {
       ConnectionManager.getInstance().checkIn(conn);
	}
	
	 public PerfilGrupo recuperarPerfilGrupoById(String idPerfil) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        PerfilGrupo p = null;
	        String sql = "SELECT * FROM perfilGrupo WHERE (idPerfilGrupo = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, idPerfil);
	            result = stmt.executeQuery();

	            if ( result.next() ){
		            p = new PerfilGrupo();
		            p.setIdPerfilGrupo(result.getString("idPerfilGrupo"));
		            p.setNombre(result.getString("nombre"));
		            p.setEmail(result.getString("email"));
		            p.setTelefono(result.getString("telefono"));
		            p.setRider(result.getString("rider"));
		            p.setComponentes(result.getString("componentes"));
	            }
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p;
	    }
	 
	 public PerfilSala recuperarPerfilSalaById(String idPerfil) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        PerfilSala p = null;
	        String sql = "SELECT * FROM perfilSala WHERE (idPerfilSala = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, idPerfil);
	            result = stmt.executeQuery();

	            if ( result.next() ){
		            p = new PerfilSala();
		            p.setIdPerfilSala(result.getString("idPerfilSala"));
		            p.setNombre(result.getString("nombre"));
		            p.setEmail(result.getString("email"));
		            p.setTelefono(result.getString("telefono"));
		            p.setDireccion(result.getString("direccion"));
		            p.setAforoMaximo(result.getInt("aforoMaximo"));
		            p.setCondiconesContratacion(result.getString("condicionesContratacion"));
		            p.setEquipoDisponible(result.getString("equiposDisponibles"));
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p;
	    }
	 
	 public PerfilConcurso recuperarPerfilConcursoById(String idPerfil) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        PerfilConcurso p = null;
	        String sql = "SELECT * FROM perfilConcurso WHERE (idPerfilConcurso = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, idPerfil);
	            result = stmt.executeQuery();

	            if ( result.next() ){
		            p = new PerfilConcurso();
		            p.setIdPerfilConcurso(result.getString("idPerfilConcurso"));
		            p.setNombre(result.getString("nombre"));
		            p.setEmail(result.getString("email"));
		            p.setTelefono(result.getString("telefono"));
		            p.setDuracion(result.getString("duracion"));
		            p.setPlazoInscripcion(result.getString("plazoInscripcion"));
		            p.setPrecioInscripcion(result.getInt("precioInscripcion"));
		            p.setPremios(result.getString("premios"));
	            }
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p;
	    }
	 
	 public PerfilFestival recuperarPerfilFestivalById(String idPerfil) {
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        PerfilFestival p = null;
	        String sql = "SELECT * FROM perfilFestival WHERE (idPerfilFestival = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, idPerfil);
	            result = stmt.executeQuery();

	            if ( result.next() ){
		            p = new PerfilFestival();
		            p.setIdPerfilFestival(result.getString("idPerfilFestival"));
		            p.setNombre(result.getString("nombre"));
		            p.setEmail(result.getString("email"));
		            p.setTelefono(result.getString("telefono"));
		            p.setDuracion(result.getString("duracion"));
		            p.setLugar(result.getString("lugar"));
		            p.setCartel(result.getString("cartel"));
		            p.setPrecio(result.getInt("precio"));
		            p.setServiciosFestival(result.getString("serviciosFestival"));
		            p.setCondiciones(result.getString("condiciones"));
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return p;
	    }
	 
	 public void actualizarPerfilGrupo(PerfilGrupo p) {
			String sql = "UPDATE perfilGrupo SET nombre = ?, email = ?, telefono = ?, rider = ?, componentes = ?  WHERE (idPerfilGrupo = ?)";
			PreparedStatement stm = null;
			try {
				
				stm = conn.prepareStatement(sql);
				stm.setString(1, p.getNombre());
				stm.setString(2, p.getEmail());
				stm.setString(3, p.getTelefono());
				stm.setString(4, p.getRider());
				stm.setString(5, p.getComponentes());
				stm.setString(6, p.getIdPerfilGrupo());
				stm.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}
	 
	 public void actualizarPerfilSala(PerfilSala p) {
			String sql = "UPDATE perfilSala SET nombre = ?, email = ?, telefono = ?, direccion = ?, aforoMaximo = ?, condicionesContratacion = ?, equipoDisponible = ?  WHERE (idPerfilSala = ?)";
			PreparedStatement stm = null;
			try {
				
				stm = conn.prepareStatement(sql);
				stm.setString(1, p.getNombre());
				stm.setString(2, p.getEmail());
				stm.setString(3, p.getTelefono());
				stm.setString(4, p.getDireccion());
				stm.setInt(5, p.getAforoMaximo());
				stm.setString(6, p.getCondiconesContratacion());
				stm.setString(7, p.getEquipoDisponible());
				stm.setString(8, p.getIdPerfilSala());
				
				stm.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}
	 
	 public void actualizarPerfilConcurso(PerfilConcurso p) {
			String sql = "UPDATE perfilConcurso SET nombre = ?, email = ?, telefono = ?, duracion = ?, plazoInscripcion = ?, precioInscripcion = ?, premios = ?  WHERE (idPerfilConcurso = ?)";
			PreparedStatement stm = null;
			try {
				
				stm = conn.prepareStatement(sql);
				stm.setString(1, p.getNombre());
				stm.setString(2, p.getEmail());
				stm.setString(3, p.getTelefono());
				stm.setString(4, p.getDuracion());
				stm.setString(5, p.getPlazoInscripcion());
				stm.setInt(6, p.getPrecioInscripcion());
				stm.setString(7, p.getPremios());
				stm.setString(8, p.getIdPerfilConcurso());
				
				stm.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}
	 
	 public void actualizarPerfilFestival(PerfilFestival p) {
			String sql = "UPDATE perfilConcurso SET nombre = ?, email = ?, telefono = ?, duracion = ?, lugar = ?, cartel = ?, precio = ?, serviciosFestival = ?, condiciones = ?  WHERE (idPerfilFestival = ?)";
			PreparedStatement stm = null;
			try {
				
				stm = conn.prepareStatement(sql);
				stm.setString(1, p.getNombre());
				stm.setString(2, p.getEmail());
				stm.setString(3, p.getTelefono());
				stm.setString(4, p.getDuracion());
				stm.setString(5, p.getLugar());
				stm.setString(6, p.getCartel());
				stm.setInt(7, p.getPrecio());
				stm.setString(8, p.getServiciosFestival());
				stm.setString(9, p.getCondiciones());
				stm.setString(10, p.getIdPerfilFestival());
				
				stm.executeUpdate();

			} catch (SQLException e) {
				System.out.println("Message: " + e.getMessage());
				System.out.println("SQLState: " + e.getSQLState());
				System.out.println("ErrorCode: " + e.getErrorCode());
			} finally {
				try {
					if (stm != null) {
						stm.close();
					}
				} catch (SQLException e) {

				}
			}
		}
}
